package org.wabson.alfresco.translations;

import java.io.InputStream;

/**
 * Load message bundle files from a ZIP/JAR file on the classpath.
 * 
 * @author wabson
 */
public class ClassPathJarLoader extends AbstractJarLoader implements JarBundleLoader
{
    /**
     * Classpath location of JAR file
     */
    protected String jarPath;
    
    /**
     * Instantiate a new file system message loader with the given base location
     * 
     * @param jarPath	Class path location of the JAR file
     * @param dirPath	Directory within the JAR file to search for message bundles
     */
    public ClassPathJarLoader(String jarPath, String dirPath)
    {
        this.jarPath = jarPath;
        this.dirPath = dirPath;
        init();
    }
    
    private void init()
    {
        InputStream jis = this.getClass().getResourceAsStream(jarPath);
        
        if (jis == null)
        {
            throw new IllegalArgumentException("The JAR file " + jarPath + " was not found");
        }
        
        init(jis);
    }
    
    public String toString()
    {
        return "jar:" + jarPath + "!" + dirPath;
    }

    public String getJarPath()
    {
        return jarPath;
    }

    public void setJarPath(String jarPath)
    {
        this.jarPath = jarPath;
    }

	@Override
	public String getFileName()
	{
		return jarPath.lastIndexOf("/") > -1 ? jarPath.substring(jarPath.lastIndexOf("/") + 1) : jarPath;
	}

	@Override
	public InputStream getInputStream() throws BundleLoadingException
	{
        return this.getClass().getResourceAsStream(jarPath);
	}
}
